package com.cardbaobao.cardbabyclient.ui.adapter;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;
import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.callback.RecycleItemClickedCallback;
import com.cardbaobao.cardbabyclient.ui.fragment.CreditFragment;
import com.cardbaobao.cardbabyclient.model.CreditCardIndexLayout;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by FHW on 2017/6/20
 */

public class CooperationBankDelegateNewAdapter extends DelegateAdapter.Adapter {
    private GridLayoutHelper mLayoutHelper;
    private List<CreditCardIndexLayout.CooperateBank> data;

    private RecycleItemClickedCallback clickedCallback;
    public void setRecycleItemClickedCallback(RecycleItemClickedCallback clickedCallback){
        this.clickedCallback=clickedCallback;
    }

    public CooperationBankDelegateNewAdapter(GridLayoutHelper layoutHelper,
                                             List<CreditCardIndexLayout.CooperateBank> data ) {
        this.mLayoutHelper = layoutHelper;
        this.data=data;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView=LayoutInflater.from(parent.getContext()).
                inflate(R.layout.item_image_text_subscript,parent,false);
        return new BankHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof BankHolder){
            CreditCardIndexLayout.CooperateBank bank = data.get(position);
            BankHolder bankHolder= (BankHolder) holder;
            bankHolder.tvBankName.setText(bank.getName_short());
            String subscript=bank.getSmall_icon();
            TextView tvSubscript=bankHolder.tvSubscript;
            if(TextUtils.isEmpty(subscript)){
                tvSubscript.setVisibility(View.INVISIBLE);
            }else{
                tvSubscript.setText(subscript);
                tvSubscript.setVisibility(View.VISIBLE);
            }
            ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL+bank.getLogo_3g(),
                    bankHolder.ivBank,
                    R.mipmap.img_default_square);
            bankHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(clickedCallback!=null){
                        clickedCallback.onRecycleItemClicked(position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return CreditFragment.TYPE_COOPERATION_BANK;
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    private class BankHolder extends RecyclerView.ViewHolder{
        private TextView tvBankName;
        private TextView tvSubscript;
        private CircleImageView ivBank;
        private BankHolder(View itemView) {
            super(itemView);
            tvBankName= (TextView) itemView.findViewById(R.id.tv_name);
            tvSubscript= (TextView) itemView.findViewById(R.id.tv_subscript);
            ivBank= (CircleImageView) itemView.findViewById(R.id.iv_logo);
        }
    }
}
